package com.kun.model;

import com.kun.enums.DirectionEnum;

/**
 * 好奇号
 */
public class Curiosity {
    /** 编号 */
    private int id;
    /** x坐标 */
    private int x;
    /** y坐标 */
    private int y;
    /** 面朝方位 */
    private String direction;

    public Curiosity(int id, int x, int y, String direction) {
        this.id = id;
        this.x = x;
        this.y = y;
        this.direction = direction;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    /**
     * 好奇号执行命令
     * @param mar
     */
    public void execCommands(Mar mar,String[] commands){
        Curiosity[][] curiosities = mar.getCuriosities();
        //1. 在地图上将当前好奇号的位置抹去
        curiosities[x][y] = null;

        // 2. 根据指令移动好奇号
        for (String command : commands) {
            switch (command){
                case "L":
                    case "R":
                    turn(command);
                    break;
                case  "M":
                    moveStep(mar);
                    break;
            }
        }

        //3. 将火星车的新位置设置到地图上
        curiosities[x][y] = this;
        //4. 记录火星车最新位置
        mar.getRecorder().put(this.id,this);
    }

    /**
     * 转向
     * @param command
     */
    public void turn(String command){
        switch (command){
            case "L":
                this.direction = DirectionEnum.getNextAntiClockwise(this.direction);
                break;
            case "R":
                this.direction = DirectionEnum.getNextClockwise(this.direction);
                break;
        }
    }

    /**
     * 移动一步
     */
    public void moveStep(Mar mar){
        Curiosity[][] curiosities = mar.getCuriosities();
        if (curiosities == null || curiosities.length == 0){
            return;
        }
        int borderX = curiosities.length-1;
        int borderY = curiosities[0].length-1;
        int newX = this.x;
        int newY = this.y;
        switch (this.direction){
            case "N":
                newY = this.y + 1;
                break;
            case "S":
                newY = this.y - 1;
                break;
            case "W":
                newX = this.x - 1;
                break;
            case "E":
                newX = this.x + 1;
                break;
        }
        // 检查移动之后的位置是否合理
        if(newX <0 || newY < 0 || newX > borderX || newY > borderY){
            System.err.println("编号为"+this.id+"的火星车，如果执行指令会被移动到火星之外，所以我拒绝执行...(ps:编号从0开始)");
            return;
        }

        // 判断位置是否冲突
        if (curiosities[newX][newY] != null){
            System.err.println("编号为"+this.id+"的火星车执行指令会碰撞到在("+newX+","+newY+")处的编号为"+curiosities[newX][newY].getId()+"火星车，所以我拒绝执行...(ps:编号从0开始)");
            return;
        }
        this.x = newX;
        this.y = newY;
    }

    @Override
    public String toString() {
        return x + " " + y + " " + direction;
    }
}
